package com.yuelife.market.controller;

import com.yuelife.market.entity.MOrder;
import com.yuelife.market.entity.MProduct;
import com.yuelife.market.service.MOrderService;
import com.yuelife.market.service.MProductService;
import com.yuelife.market.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "OrderController")
@RestController
@RequestMapping("/morder")
@CrossOrigin//解决跨域的注解  但是只能局部  不能全局 全局需要在启动类中写个bean
public class OrderController {

    @Autowired
    private UserService userService;
    @Autowired
    private MProductService mProductService;
    @Autowired
    private MOrderService mOrderService;

    /**
     * 订单中获得这个商品详情和卖家信息
     * 其实在productController中也有一个一模一样的
     * @param productSn
     * @return
     */
    @ApiOperation(value = "根据商品编号查询商品信息和卖家信息")
    @ApiImplicitParam(name = "productSn", value = "productSn商品编号", required = true, dataType = "String")
    @GetMapping("/OrderProductDetails/{goodsId}")
    public Map OrderProductDetails(@PathVariable("goodsId") String productSn){
        Map map=new HashMap();
        MProduct m= null;
        try {
            m = mProductService.selectProductAndUser(productSn);
            map.put("data",m);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data",null);
            map.put("code",500);
            return map;
        }
    }


    /**
     * 创建订单，初始化是未支付状态，30分钟过后还是未支付，那么直接变为无效订单
     * 还需要修改商品的状态为已被购买，三十分钟后没人支付又修改回去“无人购买
     * 但是在最开始之前还需要判断该商品是否已经被别人购买了，因为会存在多人购买一件商品的时候，商品只有有一个，所以不能创建多个订单
     * @param mOrder
     * @return
     */
    @ApiOperation(value = "创建订单")
    @ApiImplicitParam(name = "mOrder", value = "mOrder订单对象", required = true, dataType = "MOrder")
    @PostMapping("/createOrder")
    public Map createOrder(MOrder mOrder){
        try {

            //code=200代表订单创建成功，300代表商品告罄；500代表异常
            return mOrderService.insert(mOrder);
//            int code=mOrderService.insert(mOrder);
//            if (code==200){
//                return 200;
//            }
//            if (code==300){
//                return 300;
//            }
        } catch (Exception e) {
            e.printStackTrace();
            Map map=new HashMap();
            map.put("code",500);
            return map;
        }
    }

    /**
     * 根据商品编号查询得到订单信息
     * @param orderSn
     * @return
     */
    @ApiOperation(value = "根据订单编号查询订单")
    @ApiImplicitParam(name = "orderSn", value = "orderSn订单编号", required = true, dataType = "String")
    @GetMapping("/orderInfoByProductSn/{goodsId}")
    public Map orderInfoByProductSn(@PathVariable("goodsId") String orderSn){
        Map map=new HashMap();
        try {
            MOrder mOrder=mOrderService.orderInfoByOrderSn(orderSn);
            map.put("data",mOrder);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data",null);
            map.put("code",500);
            return map;
        }
    }

    /**
     * 查询到所有状态的订单
     * @param mOrder
     * @return
     */
    @ApiOperation(value = "根据订单状态查询订单")
    @ApiImplicitParam(name = "mOrder", value = "mOrder订单对象", required = true, dataType = "MOrder")
    @GetMapping("/orderStatus")
    public Map orderStatus(MOrder mOrder){
        Map map=new HashMap();
        try {
            List<MOrder> orderList=mOrderService.selectOrder(mOrder);
            map.put("code",200);
            map.put("data",orderList);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }

    /**
     * 确认收货之后修改订单状态  已收货之后，订单状态为 3
     * @param
     * @return
     */
    @ApiOperation(value = "确认收货，修改订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "orderSn订单编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "status订单状态", required = true, dataType = "String")
    })
    @PutMapping("/affirmGetCargo/{orderSn}/{status}")
    public int affirmGetCargo(@PathVariable("orderSn") String orderSn,@PathVariable("status") String status){
        MOrder mOrder=new MOrder();
        mOrder.setOrderSn(orderSn);
        mOrder.setOrderStatus(status);
        try {
            mOrderService.update(mOrder);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }

    /**
     * 查询卖家没有发货的商品，
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "查询没有发货的订单")
    @ApiImplicitParam(name = "mOrder", value = "mOrder订单对象", required = true, dataType = "MOrder")
    @GetMapping("/productManager")
    public Map productManager(MOrder mOrder){
        Map map=new HashMap();
        try {
            List<MOrder> mOrders=mOrderService.selectNoSendProduct(mOrder.getUserSn(),mOrder.getOrderStatus());
            map.put("code",200);
            map.put("data",mOrders);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }




}
